package com.lucifer.cloud.auth.msg.producer;

import cn.hutool.json.JSONUtil;
import com.lucifer.cloud.auth.msg.producer.domain.MsgOauth2BasicUser;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

/**
 * @author lucifer
 * @date 2023/12/26 14:41
 */
@Slf4j
@Service
public class UserProducer {

    @Resource
    private KafkaTemplate<String, Object> kafkaTemplate;


    public void sendUser2Blog(MsgOauth2BasicUser msgOauth2BasicUser) {
        String user = JSONUtil.toJsonStr(msgOauth2BasicUser);
        kafkaTemplate.send("BLOG_SAVE_USER", msgOauth2BasicUser.getName(), user)
                .thenAcceptAsync(success -> {
                    String topic = success.getRecordMetadata().topic();
                    int partition = success.getRecordMetadata().partition();
                    long offset = success.getRecordMetadata().offset();
                    log.info("从：{}人员注册给博客人员系统发送消息成功。name={},topic={},partition={},offset={}",msgOauth2BasicUser.getSourceFrom(),msgOauth2BasicUser.getName(), topic, partition, offset);
                })
                .exceptionallyAsync(throwable -> {
                    String message = throwable.getMessage();
                    log.error("从：{}人员注册给博客系统发送消息失败。name={},message={}",msgOauth2BasicUser.getSourceFrom(),msgOauth2BasicUser.getName(), message);
                    return null;
                });
    }
}
